package cn.lili.modules.payment.entity.dos;

import cn.lili.modules.payment.entity.dto.UserWalletUpdateDTO;
import cn.lili.modules.payment.entity.enums.WalletServiceTypeEnum;
import cn.lili.mybatis.model.BaseEntity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 钱包日志实体
 *
 * @author pikachu
 * @since 2020-02-25 14:10:16
 */
@Data
@TableName("li_wallet_log")
@ApiModel(value = "钱包变动日志")
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class WalletLog extends BaseEntity {

    private static final long serialVersionUID = -1599270544927161096L;

    @ApiModelProperty(value = "用户id")
    private String userId;

    @ApiModelProperty(value = "用户名称")
    private String nickName;

    @ApiModelProperty(value = "金额")
    private Double money;

    @ApiModelProperty(value = "流水号")
    private String outTradeNo;

    /**
     * @see WalletServiceTypeEnum
     */
    @ApiModelProperty(value = "业务类型")
    private String serviceType;

    @ApiModelProperty(value = "日志明细")
    private String detail;


    @ApiModelProperty(value = "订单号: 方便通过订单号筛选整个流水过程")
    private String orderSn;

    @CreatedBy
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建者", hidden = true)
    private String createBy;

    @CreatedDate
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间", hidden = true)
    private Date createTime;

    @ApiModelProperty(value = "类型:0支出、1收入")
    @TableField(exist = false)
    private Integer type;

    @ApiModelProperty(value = "类型:0支出、1收入")
    @TableField(exist = false)
    private String serviceTypeStr;

    /**
     * 构建新的预存款日志对象
     *
     * @param userWalletUpdateDTO 变动模型
     */
    public WalletLog(UserWalletUpdateDTO userWalletUpdateDTO) {
        this.setUserId(userWalletUpdateDTO.getUserId());
        this.setNickName(userWalletUpdateDTO.getNickname());
        this.setMoney(userWalletUpdateDTO.getAmount());
        this.setDetail(userWalletUpdateDTO.getDetail());
        this.setOrderSn(userWalletUpdateDTO.getOrderSn());
        this.setServiceType(userWalletUpdateDTO.getServiceType().name());
        this.setOutTradeNo(userWalletUpdateDTO.getOutTradeNo());
    }

    /**
     * 构建新的预存款日志对象
     *
     * @param nickname            用户名称
     * @param userWalletUpdateDTO 变动模型
     * @param isReduce            是否是消费
     */
    public WalletLog(String nickname, UserWalletUpdateDTO userWalletUpdateDTO, boolean isReduce) {
        this.setUserId(userWalletUpdateDTO.getUserId());
        this.setNickName(nickname);
        this.setMoney(isReduce ? -userWalletUpdateDTO.getAmount() : userWalletUpdateDTO.getAmount());
        this.setDetail(userWalletUpdateDTO.getDetail());
        this.setServiceType(userWalletUpdateDTO.getServiceType().name());
    }

}